package DataStructure;

import java.util.Scanner;

/**
 * @time: 2021/3/17/017 17:04
 * @author: Zhurunhe
 */
public class QueueTest {


    public static void main(String[] args) {

        Queue queue = new Queue(5);
        Scanner scanner = new Scanner(System.in);
        char key = ' ';
        Boolean loop = true;
        while (loop) {
            System.out.println("a:添加数据到队列中");
            System.out.println("g:从队列中取数据");
            System.out.println("h:获取队列中的头数据");
            System.out.println("s:显示队列中的数据");
            System.out.println("e:退出");
            key = scanner.next().charAt(0);
            switch (key) {
                case 'a':
                    System.out.println("请输入一个数值");
                    queue.addToQueue(scanner.nextInt());
                    break;
                case 'g':

                    try {
                        System.out.println(queue.getQuererNum());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':

                    try {
                        System.out.println(queue.getHeadQueue());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 's':
                    try {
                        queue.showQueue();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }

                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;

                default:
                    break;

            }

        }
        System.out.println("程序退出");

    }

}

class Queue {
    private int[] queueArray;
    private int maxSize;
    private int front;
    private int rear;

    public Queue(int maxSize) {
        this.maxSize = maxSize;
        queueArray = new int[maxSize];
        front = -1;
        rear = -1;
    }

    public boolean isFull() {
        return rear == maxSize - 1;
    }

    //添加数到队列中
    public void addToQueue(int n) {

        if (isFull()) {
            System.out.println("队列已满，不能添加数据");
            return;
        }
        rear++;
        queueArray[rear] = n;

    }

    public boolean isEmpty() {
        return rear == front;
    }

    public int getQuererNum() {
        if (isEmpty()) {

            throw new RuntimeException("队列为空，不能取数据");
        }
        front++;
        return queueArray[front];
    }

    public int getHeadQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空，不能取数据");
        }
        return queueArray[front + 1];
    }

    public void showQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空，不能取数据");
        }
        for (int i = 0; i < queueArray.length; i++) {
            System.out.println(queueArray[i]);
        }
    }


}